from loguru import logger
from loguru._logger import Logger

from config import logger_config
from sys import stdout
import os.path

__author__ = '冰点契约'


class QuickLogger:
    __logger = None

    def __init__(self):
        raise Exception('对象无法实例化')

    @staticmethod
    def get_logger() -> Logger:
        self = QuickLogger
        if self.__logger is None:
            self.__logger = logger
            self.__logger.remove()  # 清除日志设置
            console_log_format: str = logger_config.CONSOLE_LOGGER_CONFIG['FORMAT']
            console_log_level: str = logger_config.CONSOLE_LOGGER_CONFIG['LEVEL']
            console_log_is_open: bool = logger_config.CONSOLE_LOGGER_CONFIG['IS_OPEN']
            if console_log_is_open:
                logger.add(stdout, format=console_log_format, level=console_log_level)
            file_log_is_open: bool = logger_config.FILE_LOGGER_CONFIG['IS_OPEN']
            if file_log_is_open:
                file_log_level = logger_config.FILE_LOGGER_CONFIG['LEVEL']
                file_log_name: str = logger_config.FILE_LOGGER_CONFIG['FILE_NAME']
                file_log_encoding: str = logger_config.FILE_LOGGER_CONFIG['ENCODING']
                compression: str = logger_config.FILE_LOGGER_CONFIG['COMPRESSION']
                file_log_retention: str = logger_config.FILE_LOGGER_CONFIG['RETENTION']
                file_log_rotation: str = logger_config.FILE_LOGGER_CONFIG['ROTATION']
                file_log_format: str = logger_config.FILE_LOGGER_CONFIG['FORMAT']
                file_log_enqueue: bool = logger_config.FILE_LOGGER_CONFIG['ENQUEUE']
                if not os.path.exists(file_log_name):
                    os.makedirs(file_log_name)
                logger.add(sink=file_log_name, level=file_log_level, encoding=file_log_encoding,
                           compression=compression,
                           retention=file_log_retention, rotation=file_log_rotation, format=file_log_format,
                           enqueue=file_log_enqueue)
        return self.__logger


quick_logger = QuickLogger.get_logger()
